#################################################################################### 
####################################################################################
####################################################################################
# Appendix G: Tables G.20 - G.23, M.53-M.56
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
####################################################################################
####################################################################################
## Read in survey data (from: 8_2020_analysis.R)
dat = readRDS(here("data/dat_unrestricted.rds"))
datb = readRDS(here("data/dat_restricted.rds"))

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Bushenyi","Ibanda","Jinja","Mbale","Mpigi","Pallisa","Mbarara","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
dat[, paste0("c_", cols)] = lapply(dat[, cols], function(x) (x - mean(x))  )
covariates = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment",
               "c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment")

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Ibanda","Pallisa","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
datb[, paste0("c_", cols)] = lapply(datb[, cols], function(x) (x - mean(x))  )
covariatesb = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment", 
                "c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment")


####################################################################################
## Capacity  ####

# Unrestricted
for (i in names(dat[c("provide_ngos_st","provide_ng_st","provide_dg_st","provide_gov_index")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("provide_ngos_st","provide_ng_st","provide_dg_st","provide_gov_index")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table I.28

stargazer(m.provide_gov_index.t, bm.provide_gov_index.t, m.provide_dg_st.t, bm.provide_dg_st.t, 
          m.provide_ng_st.t, bm.provide_ng_st.t, m.provide_ngos_st.t, bm.provide_ngos_st.t,  
          
          se = list(c.provide_gov_index.t[,2], bc.provide_gov_index.t[,2], c.provide_dg_st.t[,2], bc.provide_dg_st.t[,2], 
                    c.provide_ng_st.t[,2], bc.provide_ng_st.t[,2], c.provide_ngos_st.t[,2], bc.provide_ngos_st.t[,2] ),
          
          p = list(c.provide_gov_index.t[,4], bc.provide_gov_index.t[,4], c.provide_dg_st.t[,4], bc.provide_dg_st.t[,4], 
                   c.provide_ng_st.t[,4], bc.provide_ng_st.t[,4], c.provide_ngos_st.t[,4], bc.provide_ngos_st.t[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/capacity_1_c.tex",
          label = "tab:capacity_1_c",column.sep.width = "1pt", table.placement = "!ht",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Government and NGO Capacity",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt Index","Local Govt","Natl Govt","NGOs"), column.separate = c(2,2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")))

####################################################################################
####################################################################################
## Table M.65

stargazer(m.provide_gov_index.c, bm.provide_gov_index.c, m.provide_dg_st.c, bm.provide_dg_st.c, 
          m.provide_ng_st.c, bm.provide_ng_st.c, m.provide_ngos_st.c, bm.provide_ngos_st.c,  
          
          se = list(c.provide_gov_index.c[,2], bc.provide_gov_index.c[,2], c.provide_dg_st.c[,2], bc.provide_dg_st.c[,2], 
                    c.provide_ng_st.c[,2], bc.provide_ng_st.c[,2], c.provide_ngos_st.c[,2], bc.provide_ngos_st.c[,2] ),
          
          p = list(c.provide_gov_index.c[,4], bc.provide_gov_index.c[,4], c.provide_dg_st.c[,4], bc.provide_dg_st.c[,4], 
                   c.provide_ng_st.c[,4], bc.provide_ng_st.c[,4], c.provide_ngos_st.c[,4], bc.provide_ngos_st.c[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/capacity_1_nc.tex",
          label = "tab:capacity_1_nc",column.sep.width = "1pt", table.placement = "!ht",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Government and NGO Capacity (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt Index","Local Govt","Natl Govt","NGOs"), column.separate = c(2,2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")))

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))


####################################################################################
## Spending

# Unrestricted
for (i in names(dat[c("spent_ngo_index","spent_hi_ngo_st","spent_ho_ngo_st", 
                      "spent_govt_index","spent_hi_govt_st","spent_ho_govt_st")])){ # Dependent Variables 
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("spent_ngo_index","spent_hi_ngo_st","spent_ho_ngo_st", 
                       "spent_govt_index","spent_hi_govt_st","spent_ho_govt_st")])){ # Dependent Variables 
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table I.29

stargazer(m.spent_govt_index.t, m.spent_hi_govt_st.t, m.spent_ho_govt_st.t, m.spent_ngo_index.t, m.spent_hi_ngo_st.t, m.spent_ho_ngo_st.t,
          
          se = list(c.spent_govt_index.t[,2], c.spent_hi_govt_st.t[,2], c.spent_ho_govt_st.t[,2], 
                    c.spent_ngo_index.t[,2], c.spent_hi_ngo_st.t[,2],c.spent_ho_ngo_st.t[,2]),
          
          
          p = list(c.spent_govt_index.t[,4], c.spent_hi_govt_st.t[,4], c.spent_ho_govt_st.t[,4], 
                   c.spent_ngo_index.t[,4], c.spent_hi_ngo_st.t[,4], c.spent_ho_ngo_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/capacity_2_urc.tex",
          label = "tab:capacity_2_urc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending on Service Delivery",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","In-Facility", "Out-Facility","Index", "In-Facility","Out-Facility"))

####################################################################################
####################################################################################
## Table M.66

stargazer(m.spent_govt_index.c, m.spent_hi_govt_st.c, m.spent_ho_govt_st.c, m.spent_ngo_index.c, m.spent_hi_ngo_st.c, m.spent_ho_ngo_st.c,
          
          se = list(c.spent_govt_index.c[,2], c.spent_hi_govt_st.c[,2], c.spent_ho_govt_st.c[,2], 
                    c.spent_ngo_index.c[,2], c.spent_hi_ngo_st.c[,2],c.spent_ho_ngo_st.c[,2]),
          
          
          p = list(c.spent_govt_index.c[,4], c.spent_hi_govt_st.c[,4], c.spent_ho_govt_st.c[,4], 
                   c.spent_ngo_index.c[,4], c.spent_hi_ngo_st.c[,4], c.spent_ho_ngo_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/capacity_2_urnc.tex",
          label = "tab:capacity_2_urnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending on Service Delivery (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","In-Facility", "Out-Facility","Index", "In-Facility","Out-Facility"))


####################################################################################
####################################################################################
## Table I.30

stargazer(bm.spent_govt_index.t, bm.spent_hi_govt_st.t, bm.spent_ho_govt_st.t, bm.spent_ngo_index.t, bm.spent_hi_ngo_st.t, bm.spent_ho_ngo_st.t,
          
          se = list(bc.spent_govt_index.t[,2], bc.spent_hi_govt_st.t[,2], bc.spent_ho_govt_st.t[,2], 
                    bc.spent_ngo_index.t[,2], bc.spent_hi_ngo_st.t[,2],bc.spent_ho_ngo_st.t[,2]),
          
          
          p = list(bc.spent_govt_index.t[,4], bc.spent_hi_govt_st.t[,4], bc.spent_ho_govt_st.t[,4], 
                   bc.spent_ngo_index.t[,4], bc.spent_hi_ngo_st.t[,4], bc.spent_ho_ngo_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/capacity_2_rc.tex",
          label = "tab:capacity_2_rc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending on Service Delivery (Restricted)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","In-Facility", "Out-Facility","Index", "In-Facility","Out-Facility"))

####################################################################################
####################################################################################
## Table M.67

stargazer(bm.spent_govt_index.c, bm.spent_hi_govt_st.c, bm.spent_ho_govt_st.c, bm.spent_ngo_index.c, bm.spent_hi_ngo_st.c, bm.spent_ho_ngo_st.c,
          
          se = list(bc.spent_govt_index.c[,2], bc.spent_hi_govt_st.c[,2], bc.spent_ho_govt_st.c[,2], 
                    bc.spent_ngo_index.c[,2], bc.spent_hi_ngo_st.c[,2],bc.spent_ho_ngo_st.c[,2]),
          
          
          p = list(bc.spent_govt_index.c[,4], bc.spent_hi_govt_st.c[,4], bc.spent_ho_govt_st.c[,4], 
                   bc.spent_ngo_index.c[,4], bc.spent_hi_ngo_st.c[,4], bc.spent_ho_ngo_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/capacity_2_rnc.tex",
          label = "tab:capacity_2_rnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Spending on Service Delivery (Restricted; No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","In-Facility", "Out-Facility","Index", "In-Facility","Out-Facility"))

# Post-processing
# & \multicolumn{3}{c}{Government} & \multicolumn{3}{c}{NGOs} \\ 
# \cmidrule(lr){2-4} \cmidrule(l){5-7} 

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))
